package org.codersunit.radboid.test;

import java.util.HashMap;
import java.util.Map;

public class ScheduleIndexParser extends HtmlParser {

	private String eventValidation = null;
	private String viewState = null;
	
	private boolean inSelectElement = false;
	private boolean inOptionElement = false;
	private String subjectValue = null;
	
	private HashMap<String, String> subjects = new HashMap<String, String>();
	
	public Map<String, String> getSubjects() {
		return subjects;
	}
	
	public String getEventValidation() {
		return eventValidation;
	}
	
	public String getViewState() {
		return viewState;
	}
	
	@Override
	protected void handleStartElement(String name, Map<String, String> attrs,
			boolean selfClosed) 
	{
		if(inSelectElement) {
			if("option".equals(name)) {
				inOptionElement = true;
				subjectValue = attrs.get("value");
			}
		} else {
			if("input".equals(name) && "__VIEWSTATE".equals(attrs.get("name"))) {
				viewState = attrs.get("value");
			} else if("input".equals(name) && "__EVENTVALIDATION".equals(attrs.get("name"))) {
				eventValidation = attrs.get("value");
			} else if("select".equals(name) && "dlObject".equals(attrs.get("name"))) {
				inSelectElement = true;
			}
		}
	}
	
	@Override
	protected void handleEndElement(String name) 
	{
		if(inOptionElement && "option".equals(name)) {
			inOptionElement = false;
		} else if(inSelectElement && "select".equals(name)) {
			inSelectElement = false;
			stop();
		}
	}

	@Override
	protected void handleText(String text) {
		if(inOptionElement) {
			subjects.put(subjectValue, text);
			//Log.d("RadboidTest", String.format("Code: [%s]; Name: [%s]", subjectValue, text));
		}
	}
	
	@Override
	protected void handleComment(String comment) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void handleDoctype(String doctype) {
		// TODO Auto-generated method stub
		
	}
}
